//
//  Obstacle.cpp
//  Demo
//
//  Created by CG on 13-10-7.
//
//

#include "Obstacle.h"

#include <list.h>
using namespace std;


Obstacle::Obstacle()
:BaseObject::BaseObject()
{
    m_objectClassification = ObjectClassification_Obstacle;
}
Obstacle::~Obstacle()
{

}




void Obstacle::initWithFileAndType(const char *pszFileName, ObjectType objType)
{
    //TODO: init obstacle sprite
}


void Obstacle::SetObjectPosition(const cocos2d::CCPoint &position)
{
}
void Obstacle::SetObjectScale(const float scale)
{
}
void Obstacle::SetObjectRotation(const float fRotation)
{
}



void Obstacle::CreateBody(b2World* pWorld)
{
    list<CCPoint> pointList;
    pointList.push_back(CCPointMake(120.0f / kPTM_RATIO, getWinSize().height / kPTM_RATIO));
    pointList.push_back(CCPointMake(120.0f / kPTM_RATIO, 100.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(20.0f / kPTM_RATIO, 100.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(20.0f / kPTM_RATIO, 40.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(200.0f / kPTM_RATIO, 40.0f / kPTM_RATIO));
    pointList.push_back(CCPointMake(200.0f / kPTM_RATIO, 0.0f / kPTM_RATIO));
//    
    
    b2BodyDef bodyDef;
    bodyDef.position.Set(0 / kPTM_RATIO, 0 / kPTM_RATIO);
    bodyDef.userData = this;
    
    m_pBody = pWorld->CreateBody(&bodyDef);
    //////////////////
    list<CCPoint>::iterator preIter = pointList.begin();
    
    for(list<CCPoint>::iterator iter = pointList.begin();
        iter != pointList.end();
        ++ iter)
    {
        if (preIter != iter)
        {
            b2EdgeShape edgeShape;
            edgeShape.Set(b2Vec2(((CCPoint)*preIter).x, ((CCPoint)*preIter).y),
                          b2Vec2(((CCPoint)*iter).x, ((CCPoint)*iter).y));
            
            b2FixtureDef fixtureDef;
            fixtureDef.shape = &edgeShape;
            fixtureDef.density = 0.0f;
            fixtureDef.friction = 0.5f;
            fixtureDef.restitution = 0.5f;
            m_pBody->CreateFixture(&fixtureDef);
            
            
            preIter = iter;
        }
    }
}